<template>
	<view class="all">
		<view class="biaoti">
			<view >请输入要兑换的欢乐豆总量</view>
		<view>(1积分=10豆)</view>
		</view>
		<view class="quanbu">
			 <view class="dangqianjifen">当前积分:{{user.jifen}}</view>
			 <button class="quanbuduihuan" type="primary" @click="quanbu">将积分全部兑换</button>
		</view>
		
		<input v-model="jifen" placeholder="请输入要兑换的欢乐豆总量" class="shurujifen"/>
	
			<button class="zidingyiduihuan" type="primary" @click="bufei">兑换</button>
		
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				jifen:0,
				user:{},
				zhang:''
			}
		},
		created(){
			this.zhang=getApp().globalData.zhang
			this.dangqian()
		},
		methods: {
			async dangqian(){
				
				uni.request({
				url:"http://124.221.104.5:8080/jbgn/chagerenxinxi",
				data:{zhang:this.zhang},
				success: (res) => {
					this.user=res.data
				}
			})
			await new Promise((res)=>setTimeout(res,500))
			
			},
			 quanbu(){
				uni.request({
					url:"http://124.221.104.5:8080/jbgn/duihuan",
					data:{zhang:this.zhang,xuhao:2,jifen:this.jifen},
				})
				uni.showToast({
					title:"兑换成功",
					icon:'success',
					duration:1000
				})
				this.user.jifen=0
			},
			async bufei(){
				if(this.user.jifen>=this.jifen)
				{
					uni.request({
					url:"http://124.221.104.5:8080/jbgn/duihuan",
					data:{zhang:this.zhang,xuhao:1,jifen:this.jifen},
				})
				uni.showToast({
					title:"兑换成功",
					icon:'success',
					duration:1000
				})
			  this.user.jifen-=this.jifen
				this.jifen=0
				}
				else
				{
					uni.showToast({
						title:"兑换失败",
						icon:'fail',
						duration:1000
					})
				}
			}
		}
	}
</script>

<style>
  .all{
	  position: absolute;
	   width: 601rpx;
	  height: 378rpx;
	  margin-left:150rpx ;
	  top: 0%;
	  display: flex;
	  flex-direction: column;
	  align-items: center;
	    text-align: center;
  }
  .biaoti{
	  margin-top: 20rpx;
	  font-size: 30rpx;
	
  }
   .quanbu{
	  display: flex;
	  flex-direction: row;
	  align-items: center;
	   margin-top:40rpx ;
  }
  .dangqianjifen{
	  font-size: 25rpx;
	  width: 200rpx;
  }
  .quanbuduihuan{
	  margin-left: 20rpx;
	 height:40rpx ;
	 line-height: 40rpx;
  }
  .shurujifen{
	  border: 1rpx solid aquamarine;
	  margin-top: 40rpx;
	  width: 200rpx;
	  height: 25rpx;
	  font-size: 20rpx;
  }
  .zidingyiduihuan{
	  width: 90rpx;
	  height:40rpx ;
	  line-height: 40rpx;
	  margin-top: 25rpx;
  }
 
 
</style>
